package v1

import (
	"gateway-api/controllers"
	"gateway-api/tools"
	"strconv"
	"strings"

	pbBanner "gateway-api/proto/banner"
	pbParams "gateway-api/proto/params"

	"github.com/gin-gonic/gin"
)

/*
 +----------------------------------------------------------------------
 + Title        : BannerController
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2020-11-23
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 横幅控制层
 +----------------------------------------------------------------------
*/
type BannerController struct {
	controllers.BaseController
}

/**
 * 新增横幅
 *
 * @param string title -------------------------------------------必传,标题
 * @param string image_url ---------------------------------------非必传,图片
 * @param int type -----------------------------------------------必传,类型
 * @param string link --------------------------------------------非必传,链接
 * @param string note --------------------------------------------非必传,备注
 * @param int sort -----------------------------------------------非必传,排序
 * @return Json
 * @author huwl
 */
func (control BannerController) Add(ctx *gin.Context) {
	title := ctx.DefaultPostForm("title", "")
	imageUrl := ctx.DefaultPostForm("image_url", "")
	bannerType, _ := strconv.ParseInt(ctx.DefaultPostForm("type", "0"), 10, 64)
	link := ctx.DefaultPostForm("link", "")
	note := ctx.DefaultPostForm("note", "")
	sort, _ := strconv.ParseInt(ctx.DefaultPostForm("sort", "0"), 10, 64)

	if title == "" {
		control.BaseError(ctx, control.WithMessage("请输入标题"))
		return
	}
	if !tools.IsContainNumber([]int{1, 2}, int(bannerType)) {
		control.BaseError(ctx, control.WithMessage("请选择类型"))
		return
	}

	bannerSrvClient := pbBanner.NewBannerSrvClient(tools.CommonServiceClientConn)
	_, err := bannerSrvClient.AddBanner(ctx, &pbBanner.AddBannerRequest{
		Title:    title,
		ImageUrl: imageUrl,
		Type:     uint32(bannerType),
		Link:     link,
		Note:     note,
		Sort:     uint32(sort),
	})
	if err != nil {
		control.BaseError(ctx, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(ctx, control.WithData("新增横幅成功"))
}

/**
 * 删除横幅
 *
 * @param string ids ---------------------------------------------必传,横幅ID,支持多个
 * @return Json
 * @author huwl
 */
func (control BannerController) Delete(ctx *gin.Context) {
	ids := ctx.DefaultPostForm("ids", "")

	if ids == "" {
		control.BaseError(ctx, control.WithMessage("横幅ID错误"))
		return
	}

	bannerSrvClient := pbBanner.NewBannerSrvClient(tools.CommonServiceClientConn)
	_, err := bannerSrvClient.DeleteBanner(ctx, &pbParams.IdsRequest{Ids: ids})
	if err != nil {
		control.BaseError(ctx, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(ctx, control.WithData("删除横幅成功"))
}

/**
 * 编辑横幅
 *
 * @param int id -------------------------------------------------必传,ID
 * @param string title -------------------------------------------非必传,标题
 * @param string image_url ---------------------------------------非必传,图片
 * @param int type -----------------------------------------------非必传,类型
 * @param string link --------------------------------------------非必传,链接
 * @param string note --------------------------------------------非必传,备注
 * @param int sort -----------------------------------------------非必传,排序
 * @return Json
 * @author huwl
 */
func (control BannerController) Edit(ctx *gin.Context) {
	id, _ := strconv.ParseInt(ctx.DefaultPostForm("id", "0"), 10, 64)
	title := ctx.DefaultPostForm("title", "")
	imageUrl := ctx.DefaultPostForm("image_url", "")
	bannerType, _ := strconv.ParseInt(ctx.DefaultPostForm("type", "0"), 10, 64)
	link := ctx.DefaultPostForm("link", "")
	note := ctx.DefaultPostForm("note", "")
	sort, _ := strconv.ParseInt(ctx.DefaultPostForm("sort", "0"), 10, 64)

	if id <= 0 {
		control.BaseError(ctx, control.WithMessage("横幅ID错误"))
		return
	}

	bannerSrvClient := pbBanner.NewBannerSrvClient(tools.CommonServiceClientConn)
	_, err := bannerSrvClient.EditBanner(ctx, &pbBanner.EditBannerRequest{
		Id:       uint64(id),
		Title:    title,
		ImageUrl: imageUrl,
		Type:     uint32(bannerType),
		Link:     link,
		Note:     note,
		Sort:     uint32(sort),
	})
	if err != nil {
		control.BaseError(ctx, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(ctx, control.WithData("编辑横幅成功"))
}

/**
 * 编辑横幅状态
 *
 * @param string ids ---------------------------------------------必传,横幅ID支持多个
 * @param int status ---------------------------------------------必传,状态
 * @return Json
 * @author huwl
 */
func (control BannerController) EditStatus(ctx *gin.Context) {
	ids := ctx.DefaultPostForm("ids", "")
	status, _ := strconv.ParseInt(ctx.DefaultPostForm("status", ""), 10, 64)

	if ids == "" {
		control.BaseError(ctx, control.WithMessage("横幅ID错误"))
		return
	}

	if !tools.IsContainNumber([]int{-1, 1}, int(status)) {
		control.BaseError(ctx, control.WithMessage("状态错误"))
		return
	}

	bannerSrvClient := pbBanner.NewBannerSrvClient(tools.CommonServiceClientConn)
	result, err := bannerSrvClient.EditBannerStatus(ctx, &pbParams.EditStatusRequest{
		Ids:    strings.TrimSuffix(ids, ","),
		Status: status,
	})
	if err != nil {
		control.BaseError(ctx, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(ctx, control.WithData(result.Msg))
}

/**
 * 横幅列表
 *
 * @param int id -------------------------------非必传,主键
 * @param string ids ---------------------------非必传,多个主键
 * @param string title -------------------------非必传,标题
 * @param string status ------------------------非必传,状态
 * @param string type --------------------------非必传,类型
 * @param string start_create_ime --------------非必传,开始创建时间
 * @param string end_create_time ---------------非必传,结束创建时间
 * @param string sort_type ---------------------非必传,排序方式
 * @param string sort_field --------------------非必传,排序字段
 * @param int page_size ------------------------非必传,条数
 * @param int page -----------------------------非必传,页码
 * @return Json
 * @author huwl
 */
func (control BannerController) List(ctx *gin.Context) {
	id, _ := strconv.ParseInt(ctx.DefaultQuery("id", "0"), 10, 64)
	ids := ctx.DefaultQuery("ids", "")
	title := ctx.DefaultQuery("title", "")
	status := ctx.DefaultQuery("status", "all")
	bannerType := ctx.DefaultQuery("type", "all")
	startCreateTime := ctx.DefaultQuery("start_create_ime", "")
	endCreateTime := ctx.DefaultQuery("end_create_time", "")
	sortField := ctx.DefaultQuery("sort_field", "id")
	sortType := ctx.DefaultQuery("sort_type", "desc")
	pageSize, _ := strconv.ParseInt(ctx.DefaultQuery("page_size", "20"), 10, 64)
	page, _ := strconv.ParseInt(ctx.DefaultQuery("page", "1"), 10, 64)

	bannerSrvClient := pbBanner.NewBannerSrvClient(tools.CommonServiceClientConn)
	result, err := bannerSrvClient.BannerList(ctx, &pbBanner.QueryBannerRequest{
		Id:              uint64(id),
		Ids:             ids,
		Title:           title,
		Status:          status,
		Type:            bannerType,
		StartCreateTime: startCreateTime,
		EndCreateTime:   endCreateTime,
		SortType:        sortType,
		SortField:       sortField,
		Page:            uint32(page),
		PageSize:        uint32(pageSize),
	})
	if err != nil {
		control.BaseError(ctx, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{
		"page":       result.ListResult.Page,
		"page_total": result.ListResult.PageTotal,
		"total":      result.ListResult.Total,
		"banners":    result.Banners,
	}))
}

/**
 * 横幅详细
 *
 * @param int id -------------------------------必传,横幅ID
 * @return Json
 * @author huwl
 */
func (control BannerController) Detail(ctx *gin.Context) {
	id, _ := strconv.ParseInt(ctx.DefaultQuery("id", "0"), 10, 64)
	if id <= 0 {
		control.BaseError(ctx, control.WithMessage("横幅广告ID错误"))
		return
	}

	bannerSrvClient := pbBanner.NewBannerSrvClient(tools.CommonServiceClientConn)
	result, err := bannerSrvClient.BannerDetail(ctx, &pbParams.DetailRequest{Id: id})
	if err != nil {
		control.BaseError(ctx, control.WithGrpcError(err))
		return
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{"banner": result.Banner}))
}
